package com.xxxx.server.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.xxxx.server.mapper.EmployeeMapper;
import com.xxxx.server.mapper.SalaryMapper;
import com.xxxx.server.pojo.Employee;
import com.xxxx.server.pojo.RespBean;
import com.xxxx.server.pojo.Salary;
import com.xxxx.server.service.IEmployeeService;
import com.xxxx.server.service.ISalaryService;
import com.xxxx.server.vo.EmpSal;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author zhoubin
 * @since 2020-10-15
 */
@Service
public class SalaryServiceImpl extends ServiceImpl<SalaryMapper, Salary> implements ISalaryService {
    @Autowired
    private  SalaryMapper salaryMapper;

    @Autowired
    private EmployeeMapper employeeMapper;

    @Autowired
    private IEmployeeService employeeService;


    /**
     * 查询所有的工资账套
     * @return
     */
    @Override
    public List<Salary> list() {
        return salaryMapper.selectList(new QueryWrapper<Salary>().apply("createDate is not null"));
    }

    /**
     * 添加工资账套
     * @param salary
     * @return
     */
    @Override
    @Transactional
    public RespBean saveSalary(Salary salary) {
        RespBean respBean = dataChecked(salary);
        if (respBean.getCode() != 200)
            return respBean;
        //判断是否存在
        List<Object> names = salaryMapper.selectObjs(new QueryWrapper<Salary>().select("name").apply("createDate is not null"));
        for(Object name : names){
            if(name.equals(salary.getName()))
                return RespBean.error("工资账套已存在，添加失败");
        }

        //执行插入操作，返回影响行数
        salary.setCreateDate(LocalDateTime.now());
        int insert = salaryMapper.insert(salary);
        if(insert < 1){
            return RespBean.error("系统异常，添加失败");
        }
        return RespBean.success("添加成功");
    }

    /**
     * 更新工资账套
     * @param salary
     * @return
     */
    @Transactional
    public RespBean editSalary(Salary salary){
        //数据校验
        RespBean respBean = dataChecked(salary);
        if (respBean.getCode() != 200)
            return respBean;

        //更新工资账套
        int result = salaryMapper.updateById(salary);
        if(result < 1)
            return RespBean.error("系统异常，更新失败");
        return RespBean.success("更新成功");


    }

    /**
     * 通过id删除对应的工资账套
     * @param id
     * @return
     */
    @Override
    @Transactional
    public RespBean delete(Integer id) {
        if(id ==null || id == 0){
            return RespBean.error("参数传递错误");
        }
        Salary salary = salaryMapper.selectById(id);
        salary.setCreateDate(null);
        //删除员工表中的相应的工资账套id
        List<Employee> employees = employeeMapper.selectList(new QueryWrapper<Employee>().eq("salaryId",id));
        if(employees != null){
            employees.forEach(e -> e.setSalaryId(null));
        }
        boolean b = employeeService.updateBatchById(employees);
        if(!b){
            return RespBean.error("系统异常，删除失败");
        }
        //删除工资表中的对应的工资账套
        int i = salaryMapper.updateById(salary);
        if(i < 1){
            return RespBean.error("系统异常，删除失败");
        }
        return RespBean.success("删除成功");
    }

    /**
     * 查询所有员工及其工资账套信息
     * @param currentPage
     * @param size
     * @return
     */
    @Override
    public Map<String, Object> empSalList(Integer currentPage, Integer size) {
        PageHelper.startPage(currentPage,size);
        List<EmpSal> empSals = salaryMapper.empSalaries();
        PageInfo<EmpSal> pageInfo =PageInfo.of(empSals);
        Map<String, Object> map = new HashMap<>();
        map.put("total", pageInfo.getTotal());
        map.put("data", pageInfo.getList());
        return map;
    }

    /**
     * 更新员工工资账套
     * @param eid 员工id
     * @param sid 工资账套id
     * @return
     */
    @Override
    @Transactional
    public RespBean updateEmpSal(Integer eid, Integer sid) {
        if(eid == null) return RespBean.error("参数错误,更新失败");
        Employee employee = employeeMapper.selectById(eid);
        if(employee == null) return RespBean.error("用户不存在,更新失败");
        employee.setSalaryId(sid);
        int i = employeeMapper.updateById(employee);
        if(i < 1){
            return RespBean.error("系统异常，删除失败");
        }
        return RespBean.success("修改成功");
    }


    /**
     * 数据校验
     * @param salary
     * @return
     */
    private RespBean dataChecked(Salary salary){
        //判断工资账套名字是否为空
        if(salary == null || salary.getName()==null || "".equals(salary.getName())){
            return RespBean.error("参数为空，添加失败");
        }
        //判断数据是否为有效数字
        if(salary.getAccumulationFundPer() != null && (salary.getAccumulationFundPer() > 1 || salary.getAccumulationFundPer() < 0)){
            return RespBean.error("参数异常，添加失败");
        }
        if(salary.getMedicalPer() != null && (salary.getMedicalPer() > 1 || salary.getMedicalPer() < 0)){
            return RespBean.error("参数异常，添加失败");
        }
        if(salary.getPensionPer() != null && (salary.getPensionPer() > 1 || salary.getPensionPer() < 0)){
            return RespBean.error("参数异常，添加失败");
        }
        return RespBean.success("参数正常");
    }

}
